package com.ricohgag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ricohgag.entity.ShopOrder;
import com.ricohgag.entity.UserAddress;
import com.ricohgag.entity.custom.ShopOrderGoodsVo;
import com.ricohgag.entity.custom.ShopOrderVo;
import com.ricohgag.mapper.ShopOrderMapper;
import com.ricohgag.service.ShopOrderGoodsService;
import com.ricohgag.service.ShopOrderService;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Service
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder> implements ShopOrderService {
    @Autowired
    private ShopOrderMapper shopOrderMapper;
    @Autowired
    private ShopOrderGoodsService shopOrderGoodsService;

    @Override
    public int insertShopOrder( String orderSn ,UserAddress address, BigDecimal moneySum, String message) throws Exception{
        ShopOrder shopOrder = new ShopOrder();
        shopOrder.setOrderSn(orderSn);

        shopOrder.setAddTime(LocalDateTime.now());
        shopOrder.setUserName(address.getUserName());
        shopOrder.setProvince(address.getProvince());
        shopOrder.setCity(address.getCity());
        shopOrder.setArea(address.getArea());
        shopOrder.setAddress(address.getAddress());
        shopOrder.setPhone(address.getPhone());
        shopOrder.setEmail(address.getCode());
        shopOrder.setShopState(1);
        shopOrder.setUserId(address.getUserId());
        shopOrder.setTotalMoney(moneySum);
        shopOrder.setRemark(message);
        System.err.println("moneySum: "+moneySum);
        return shopOrderMapper.insert(shopOrder);
    }

    @Override
    public List<ShopOrderVo> selectOrder(String orderSn, Page<ShopOrder> page) throws Exception{

        QueryWrapper<ShopOrder> qw = new QueryWrapper<>();
        if(orderSn != null){
            qw.like("order_sn", orderSn);
        }

        IPage<ShopOrder> shopAdIPage = shopOrderMapper.selectPage(page, qw);
        Assert.assertSame(page, shopAdIPage);

        //通过循环订单得到订单中的商品，赋值给包装类
        List<ShopOrderVo> shopOrderVos = new ArrayList<>();
        for (ShopOrder shopOrder:shopAdIPage.getRecords()){
            ShopOrderVo shopOrderVo = new ShopOrderVo();
            shopOrderVo.setShopOrder(shopOrder);
            List<ShopOrderGoodsVo> orderGoodsList = shopOrderGoodsService.selectGoodsByOrderId(shopOrder.getId());
            shopOrderVo.setOrderGoodsListVo(orderGoodsList);
            shopOrderVos.add(shopOrderVo);
        }

        return shopOrderVos;
    }

    @Override
    public ShopOrder selectOrderBySn(String orderSn) throws Exception{
        return shopOrderMapper.selectList(new QueryWrapper<ShopOrder>().eq("order_sn", orderSn)).get(0);
    }

    @Override
    public ShopOrder selectOrderById(Integer id) throws Exception{
        return shopOrderMapper.selectById(id);
    }

    @Override
    public IPage<ShopOrder> selectOrderByUserId(Integer userId, Page<ShopOrder> page) throws Exception{
        QueryWrapper<ShopOrder> qw = new QueryWrapper<>();
        if(userId != null){
            qw.eq("user_id", userId);
        }

        IPage<ShopOrder> shopOrderIPage = shopOrderMapper.selectPage(page, qw);
        Assert.assertSame(page, shopOrderIPage);

        return shopOrderIPage;

    }

    @Override
    public boolean updateStatusById(Integer orderId, Integer state){
        UpdateWrapper<ShopOrder> uw = new UpdateWrapper<>();
        uw.eq("id", orderId);
        ShopOrder shopOrder = shopOrderMapper.selectById(orderId);
        shopOrder.setShopState(state);
        int count = shopOrderMapper.update(shopOrder, uw);

        return count>0;
    }
}
